package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentDetailMapper;
import com.zhijian.medical.entity.po.AssessmentDetail;
import com.zhijian.medical.entity.vo.request.AssessmentDetailSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentDetailVo;
import com.zhijian.medical.service.AssessmentDetailService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-题目明细表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-02-15 15:34:47
 */
@Slf4j
@Service
public class AssessmentDetailServiceImpl implements AssessmentDetailService {

    @Autowired
    private AssessmentDetailMapper mapper;

    @Override
    public AssessmentDetail selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentDetailVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByAssessmentId(Long assessmentId) {
        mapper.deleteByAssessmentId(assessmentId);
    }

    @Override
    public Long insert(AssessmentDetail assessmentDetail) {
        Date now = new Date();
        assessmentDetail.setCreateTime(now);
        assessmentDetail.setUpdateTime(now);
        return mapper.insert(assessmentDetail) > 0 ? assessmentDetail.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentDetail assessmentDetail) {
        Date now = new Date();
        assessmentDetail.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentDetail) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentDetail record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AssessmentDetailSearchVo assessmentDetailSearchVo) {
        return mapper.count(assessmentDetailSearchVo);
    }

    @Override
    public List<AssessmentDetailVo> selectList(AssessmentDetailSearchVo assessmentDetailSearchVo) {
        return mapper.selectList(assessmentDetailSearchVo);
    }

    @Override
    public Page<AssessmentDetailVo> selectPage(AssessmentDetailSearchVo assessmentDetailSearchVo) {
        //先直接查总数
        int count = count(assessmentDetailSearchVo);
        //分页
        Page<AssessmentDetailVo> page = new Page<>(true, count, assessmentDetailSearchVo.getPageNum(), assessmentDetailSearchVo.getPageSize());
        assessmentDetailSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentDetailVo> responseVos = this.selectList(assessmentDetailSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentDetail> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public AssessmentDetailVo selectByAssessmentId(Long assessmentId) {
        List<AssessmentDetailVo> assessmentDetailVos = this.selectList(AssessmentDetailSearchVo.builder()
                .assessmentId(assessmentId)
                .build());
        return CollectionUtil.isNotEmpty(assessmentDetailVos) ? assessmentDetailVos.get(0) : null;
    }

}